﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;


namespace Marmat.Forms
{
    [DefaultEvent("Click")]
    [DefaultProperty("Caption")]
    [DesignTimeVisible(false)]
    [ToolboxItem(false)]
    public abstract class BarItem
    {
        internal DevExpress.XtraBars.BarItem barItem { get; set; }
        internal DevExpress.XtraBars.LinkPersistInfo LinkPersistInfo { get; set; }

        public BarItem(DevExpress.XtraBars.BarItem Item)
        {
            this.barItem = Item;
            this.barItem.Tag = this;
            this.LinkPersistInfo = new DevExpress.XtraBars.LinkPersistInfo(Item);
        }

        public BarItem OwnerItem
        {
            get
            {
                return this._OwnerItem;
            }
        }

        internal BarItem _OwnerItem;

        [DefaultValue(false)]
        public bool BeginGroup
        {
            get
            {
                return this.LinkPersistInfo.BeginGroup;
            }

            set
            {
                this.LinkPersistInfo.BeginGroup = value;
            }
        }

        /// <summary>
        /// 获取或设置一个值，该值指示是否显示该控件及其所有子控件。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(true)]
        [Category("行为")]
        [Description("获取或设置一个值，该值指示是否显示该控件及其所有子控件。")]
        public bool Visible
        {
            get
            {
                return this._Visible;
            }

            set
            {
                this._Visible = value;
                if (this.barItem is object)
                {
                    this.barItem.Visibility = value ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
                }
            }
        }

        private bool _Visible = true;

        [DefaultValue(true)]
        public bool Enabled
        {
            get
            {
                return this.barItem.Enabled;
            }

            set
            {
                this.barItem.Enabled = value;
            }
        }

        [DefaultValue(null)]
        public string Name
        {
            get
            {
                return this._Name;
            }

            set
            {
                this._Name = value;
                if (!string.IsNullOrEmpty(this._Name) && string.IsNullOrEmpty(this._Caption))
                {
                    this.barItem.Caption = value;
                }
            }
        }

        private string _Name;

        public string Caption
        {
            get
            {
                if (string.IsNullOrEmpty(this._Caption))
                {
                    return this.Name;
                }

                return this._Caption;
            }

            set
            {
                this._Caption = value;
                this.barItem.Caption = value;
            }
        }

        private string _Caption;

        [DefaultValue(null)]
        public Image Glyph
        {
            get
            {
                return this._Glyph;
            }

            set
            {
                this._Glyph = value;
                if (this.barItem is object)
                {
                    this.barItem.Glyph = value;
                }
            }
        }

        private Image _Glyph;

        [DefaultValue(null)]
        public virtual BarShortcut ItemShortcut
        {
            get
            {
                return this._ItemShortcut;
            }

            set
            {
                this._ItemShortcut = value;
                this.barItem.ItemShortcut = value;
            }
        }

        private BarShortcut _ItemShortcut;

        public event ClickEventHandler Click;

        public delegate void ClickEventHandler(object sender, EventArgs e);

        /// <summary>
        /// 获取或设置包含有关控件的数据的对象。
        /// </summary>
        /// <value></value>
        /// <returns>一个 System.Object，它包含有关控件的数据。默认为 null。</returns>
        /// <remarks></remarks>
        [DefaultValue(null)]
        public object Tag { get; set; }

        public void ResetCaption()
        {
            this.Caption = null;
        }

        protected bool ShouldSerializeCaption()
        {
            return !string.IsNullOrEmpty(this._Caption);
        }

        public void ResetItemShortcut()
        {
            this.ItemShortcut = null;
        }

        protected bool ShouldSerializeItemShortcut()
        {
            return !(this.ItemShortcut is null || this.ItemShortcut.Key == default(int) && this.ItemShortcut.SecondKey == default(int));
        }
    }
}